package com.oromwin.repos.client;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;

import com.oromwin.repos.common.Logger;
import com.oromwin.repos.common.ReposException;
import com.oromwin.repos.data.FileTable;
import com.oromwin.repos.data.MediaTable;
import com.oromwin.repos.data.VolumeTable;

public class SimpleReposClient {
    private static Log log = Logger.getLog(SimpleReposClient.class);
    private static final String repository = "repository";
    private static final String volume = "volume";
    private static final String media = "media";
    private static final String file = "file";
    private static final String create = "create";
    private static final String read = "read";
    private static final String update = "update";
    private static final String delete = "delete";
    private static final String copy = "copy";
    private static final String move = "move";
    private static final String info = "info";
    private static final String list = "list";
    private static final String rename = "rename";

    private static String _hostname = null;
    private static int _port = 0;
    private static String _command = null;
    private static String _target = null;
    private static String _objectId = null;

    private static ReposSocketClient _client = null;

    public static void printUsage(String params) {
        StringBuffer sb = new StringBuffer();
        sb.append("java ").append(SimpleReposClient.class.getName()).append(" [ hostname ] [ port ]\n\t [ ");
        sb.append(create).append(" | ").append(read).append(" | ").append(update).append(" | ").append(delete).append(" | ").append(copy).append(" | ").append(move).append(" | ").append(info).append(" | ").append(list).append(" ] \n\t [ ");
        sb.append(repository).append(" | ").append(volume).append(" | ").append(media).append(" | ").append(file).append(" ] \n\t [ ");
        sb.append(params).append(" ]");
        System.out.println(sb.toString());
    }

    public static void main(String[] args) {
        int argc = args.length;
        if (argc < 4) {
            printUsage("name or path");
            System.exit(0);
        }
        try {
            _hostname = args[0];
            _port = Integer.parseInt(args[1]);
            _command = args[2];
            _target = args[3];
            _client = new ReposSocketClient(_hostname, _port);
            
            if (_target.equalsIgnoreCase(repository)) {
                if (argc == 4) {
                    if (_command.equalsIgnoreCase(list)) {
                        List volumeList = _client.listRepository();
                        Iterator iterator = volumeList.iterator();
                        while (iterator.hasNext()) {
                            Object object = iterator.next();
                            if (object instanceof VolumeTable) {
                                VolumeTable volumeTable = (VolumeTable) object;
                                log.info(volumeTable.getVolumeName() + ":" + volumeTable.getVolumeId());
                            } else {
                                log.error("Casting exception: Insufficient VolumeTable object instance, " + object.getClass().toString());
                            }
                        }
                    } else {
                        SimpleReposClient.printUsage("");
                    }
                } else {
                    SimpleReposClient.printUsage("");
                }
            } else if (_target.equalsIgnoreCase(volume)) {
                if (argc == 5) {
                    String arg1 = args[4];
                    if (_command.equalsIgnoreCase(create)) {
                        _objectId = _client.createVolume(arg1);
                        log.info("created volumeId is " + _objectId);
                    } else if (_command.equalsIgnoreCase(delete)) {
                        _client.deleteVolume(arg1, true);
                    } else if (_command.equalsIgnoreCase(info)) {
                        VolumeTable volumeTable = _client.infoVolume(arg1);
                        log.info(volumeTable.toString());
                    } else if (_command.equalsIgnoreCase(list)) {
                        List mediaList = _client.listVolume(arg1);
                        Iterator iterator = mediaList.iterator();
                        while (iterator.hasNext()) {
                            Object object = iterator.next();
                            if (object instanceof MediaTable) {
                                MediaTable mediaTable = (MediaTable) object;
                                log.info(mediaTable.getMediaPath());
                            } else {
                                log.error("Casting exception: Insufficient MediaTable object instance, " + object.getClass().toString());
                            }
                        }
                    } else {
                        SimpleReposClient.printUsage("VolumeName");
                    }
                } else {
                    printUsage("name or path");
                }
            } else if (_target.equalsIgnoreCase(media)) {
                if (_command.equalsIgnoreCase(create)) {
                    if (argc == 7) {
                        String arg1 = args[4];
                        File arg2 = new File(args[5]);
                        long arg3 = Long.parseLong(args[6]);
                        _objectId = _client.createMedia(arg1, arg2, arg3);
                        log.info("created mediaId is " + _objectId);
                    } else {
                        SimpleReposClient.printUsage("VolumeId MediaPath MediaMaxBytes");
                    }
                } else if (_command.equalsIgnoreCase(update)) {
                    if (argc == 6) {
                        String arg1 = args[4];
                        long arg2 = Long.parseLong(args[5]);
                        _client.updateMedia(arg1, arg2);
                    } else {
                        SimpleReposClient.printUsage("VolumeId MediaId MediaMaxBytes");
                    }
                } else if (_command.equalsIgnoreCase(delete)) {
                    if (argc == 5) {
                        String arg1 = args[4];
                        _client.deleteMedia(arg1, true);
                    } else {
                        SimpleReposClient.printUsage("MediaId");
                    }
                } else if (_command.equalsIgnoreCase(info)) {
                    if (argc == 5) {
                        String arg1 = args[4];
                        MediaTable mediaTable = _client.infoMedia(arg1);
                        log.info(mediaTable.toString());
                    } else {
                        SimpleReposClient.printUsage("MediaId");
                    }
                } else if (_command.equalsIgnoreCase(list)) {
                    if (argc == 5) {
                        String arg1 = args[4];
                        List fileList = _client.listMedia(arg1);
                        Iterator iterator = fileList.iterator();
                        while (iterator.hasNext()) {
                            Object object = iterator.next();
                            if (object instanceof FileTable) {
                                FileTable mediaTable = (FileTable) object;
                                log.info(mediaTable.getFullFilepath());
                            } else {
                                log.error("Casting exception: Insufficient FileTable object instance, " + object.getClass().toString());
                            }
                        }
                    } else {
                        SimpleReposClient.printUsage("MediaId");
                    }
                } else {
                    printUsage("name or path");
                }
            } else if (_target.equalsIgnoreCase(file)) {
                if (_command.equalsIgnoreCase(create)) {
                    if (argc == 6) {
                        String arg1 = args[4];
                        File arg2 = new File(args[5]);
                        _objectId = _client.createFile(arg1, arg2);
                        log.info("created fileId is " + _objectId);
                    } else {
                        SimpleReposClient.printUsage("VolumeId FilePath");
                    }
                } else if (_command.equalsIgnoreCase(read)) {
                    if (argc == 6) {
                        String arg1 = args[4];
                        File arg2 = new File(args[5]);
                        _client.readFile(arg1, arg2);
                    } else {
                        SimpleReposClient.printUsage("FileId LocalFilePath");
                    }
                } else if (_command.equalsIgnoreCase(delete)) {
                    if (argc == 5) {
                        String arg1 = args[4];
                        _client.deleteFile(arg1, true);
                    } else {
                        SimpleReposClient.printUsage("FileId");
                    }
                } else if (_command.equalsIgnoreCase(copy)) {
                    if (argc == 6) {
                        String arg1 = args[4];
                        String arg2 = args[5];
                        _objectId = _client.copyFile(arg1, arg2);
                        log.info("copied fileId is " + _objectId);
                    } else {
                        SimpleReposClient.printUsage("FileId VolumeId");
                    }
                } else if (_command.equalsIgnoreCase(move)) {
                    if (argc == 6) {
                        String arg1 = args[4];
                        String arg2 = args[5];
                        _objectId = _client.moveFile(arg1, arg2);
                        log.info("moved fileId is " + _objectId);
                    } else {
                        SimpleReposClient.printUsage("FileId VolumeId");
                    }
                } else if (_command.equalsIgnoreCase(info)) {
                    if (argc == 5) {
                        String arg1 = args[4];
                        FileTable fileTable = _client.infoFile(arg1);
                        log.info(fileTable.toString());
                    } else {
                        SimpleReposClient.printUsage("FileId LocalFilePath");
                    }
                } else if (_command.equalsIgnoreCase(rename)) {
                    if (argc == 6) {
                        String arg1 = args[4];
                        String arg2 = args[5];
                        _client.renameFile(arg1, arg2);
                        log.info("rename file to " + arg2);
                    } else {
                        SimpleReposClient.printUsage("FileId RenameFileName");
                    }
                } else {
                    printUsage("name or path");
                }
            } else {
                printUsage("name or path");
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (ReposException e) {
            e.printStackTrace();
        }
        log.info("EXIT");
        System.exit(0);
    }
}
